package com.chuangyang.fixboxmaster.ui;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.baidu.mapapi.SDKInitializer;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.model.LatLng;
import com.chuangyang.fixboxlib.utils.LocationUtils;
import com.chuangyang.fixboxmaster.R;
import com.chuangyang.fixboxmaster.bean.AddressInfo;

/**
 * Created by vanshu on 15/8/31 13:34.
 */
public class MapViewActivity extends BaseActivity {
    MapView mMapView = null;
    private AddressInfo.Address mOrderAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mOrderAddress = (AddressInfo.Address) getIntent().getSerializableExtra("address");
        if (mOrderAddress == null) {
            finish();
            return;
        }
        SDKInitializer.initialize(getApplicationContext());
        setContentView(R.layout.activity_map_view);
        mMapView = (MapView) findViewById(R.id.bmapView);
        BaiduMap mBaiduMap = mMapView.getMap();
        mBaiduMap.setMaxAndMinZoomLevel(19f, 10f);
        //普通地图
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);
        //定义Maker坐标点
        if (mOrderAddress.latitude == 0 || mOrderAddress.longitude == 0) {
            Toast.makeText(this, "该订单没有经纬度", Toast.LENGTH_SHORT).show();
            finish();
            return;
        }
        LatLng pointStart =  new LatLng(LocationUtils.latitude, LocationUtils.longitude);
        LatLng pointEnd =new LatLng(mOrderAddress.latitude, mOrderAddress.longitude);
        BitmapDescriptor bitmapStart = BitmapDescriptorFactory.fromResource(R.drawable.ic_map_start);
        BitmapDescriptor bitmapEnd = BitmapDescriptorFactory.fromResource(R.drawable.ic_map_end);
        OverlayOptions optionStart = new MarkerOptions().position(pointStart).icon(bitmapStart);
        OverlayOptions optionEnd = new MarkerOptions().position(pointEnd).icon(bitmapEnd);
        mBaiduMap.addOverlay(optionStart);
        mBaiduMap.addOverlay(optionEnd);



        LatLng llText = new LatLng(mOrderAddress.latitude, mOrderAddress.longitude);
        //构建文字Option对象，用于在地图上添加文字
        OverlayOptions textOption = new TextOptions().fontSize(24).fontColor(0XFF006c).text(mOrderAddress.address1)
                .position(llText);
        //在地图上添加该文字对象并显示
        mBaiduMap.addOverlay(textOption);

        MapStatus mMapStatus = new MapStatus.Builder().target(pointStart).zoom(15).build();
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
        mBaiduMap.setMapStatus(mMapStatusUpdate);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mMapView.onDestroy();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mMapView.onPause();
    }
}
